package com.mindata.blockchain.socket.raft.utils;



import com.mindata.blockchain.socket.raft.core.proto.RaftMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * User: jasperxgwang
 * Date: 2018-8-16 9:58
 */
public class RaftUtils {

    /**
     * 127.0.0.1:5001:1,127.0.0.1:5002:2
     *
     * @param clusterPeers
     * @return
     */
    public static List<RaftMessage.Server> parseServers(String clusterPeers) {
        List<RaftMessage.Server> serverList = new ArrayList<>();
        String[] splitArray1 = clusterPeers.split(",");
        for (String addr : splitArray1) {
            String[] splitArray2 = addr.split(":");
            RaftMessage.EndPoint endPoint = RaftMessage.EndPoint.newBuilder()
                    .setHost(splitArray2[0])
                    .setPort(Integer.parseInt(splitArray2[1])).build();
            RaftMessage.Server server = RaftMessage.Server.newBuilder()
                    .setEndPoint(endPoint)
                    .setServerId(Integer.parseInt(splitArray2[2])).build();
            serverList.add(server);
        }
        return serverList;
    }

    /**
     * @param clusterPeer 127.0.0.1:5001:1
     * @return
     */
    public static RaftMessage.Server parseServer(String clusterPeer) {
        return parseServers(clusterPeer).get(0);
    }
}
